package com.totoro.rabbitmq.consumer.queue;

import com.rabbitmq.client.Channel;
import com.totoro.rabbitmq.RabbitMQProperties;
import com.totoro.rabbitmq.msg.Student;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@RabbitListener(containerFactory = "simpleRabbitListenerContainerFactory" ,
        bindings = {
                @QueueBinding(exchange = @Exchange(value=RabbitMQProperties.exchange.EXCHANGE_FANOUT_AMQ, type = ExchangeTypes.FANOUT, durable = "true"), value = @Queue(value = RabbitMQProperties.queue.QUEUE_ADD, durable = "true")),
                @QueueBinding(exchange = @Exchange(value=RabbitMQProperties.exchange.EXCHANGE_DIRECT_AMQ, type = ExchangeTypes.DIRECT, durable = "true"), value = @Queue(value = RabbitMQProperties.queue.QUEUE_ADD, durable = "true"), key = "exchange_add_direct") }
)
public class AddListener {

    @RabbitHandler
    public void handler(@Payload String msg , @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag, Channel channel){
        System.out.println("add queue msg is : "+msg);
        try {
            channel.basicAck(deliveryTag, false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RabbitHandler
    public void handlerStudent(Student student, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag, Channel channel){
        System.out.println("add queue student msg is "+ student.getName());
        try {
            channel.basicAck(deliveryTag, false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
